<?php
/**
 * 栏目分类模型
 */
header('Content-type:text/html;charset="utf-8";');
class CategoryModel extends Model{
	public $table = 'category';
	public $validate = array(
			 
		);
	//批量添加栏目接口
	public function batchAdd(){
		$this->validate = array(
//			  array('pid', 'nonull', ' pid不能为空 ',2,3),
			  array('categoryName', 'nonull', ' 栏目名不能为空 ',2,3)
		);
		if(!$this->create()) return false;
			$pid = (int)Q('post.pid');
			$cate = Q('post.categoryName');
			//用“|”分割字符串转成数组
			$cate = explode("|", $cate);
			//取出数组中的空值
			$cate = array_filter($cate);
			//查询栏目名是否重复  先验证都通过后再添加
			$categoryName = M('category')->all();			
//			foreach($cate as $k =>$v){
//				foreach($categoryName as $key =>$value){
//					if(in_array($v, $value)){
//						$this->error = '栏目名【 '.$v.' 】已存在,不能重复添加';
//						return false;
//					}
//				}
//			}
			//遍历添加栏目
			foreach($cate as $k => $v){
				
					$data['category_name'] = $v;
					$data['pid'] = $pid;
					$this->add($data);	
						
		
			}
		return true;
	}
	public function addCategory(){
		$this->validate = array(
			array('category_name','nonull','分类名称不能为空',2,3),
			array('category_name','maxlen:20','分类名称不能超过20个字符',2,3),
		);
		//自动验证
		if(!$this->create())return false;
		//栏目名重复判断
		$categoryName =Q('post.category_name');
		$data = $this->where("category_name = '{$categoryName}'")->find();
		if(isset($_GET['cid'])){
			$_POST['pid'] = Q('get.cid','0','intval');
		}
		if(!$data){
			//执行自动添加
			return $this->add();
		}else{
			$this->error="栏目名已存在!";
			return false;
		}
		
	}
	//修改栏目
	public function editCategory(){
		$this->validate = array(
			array('category_name','nonull','分类名称不能为空',2,3),
			array('category_name','maxlen:20','分类名称不能超过20个字符',2,3),
		);
		//自动验证
		if(!$this->create())return false;
		//处理提交事件
		$cid =Q('get.cid','0','intval');
		 $this->where("cid = '{$cid}'")->update();
		 return true;
	}

}
